package com.houdoudou.customer.buy;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import com.houdoudou.bean.Order;
import com.houdoudou.bean.OrderProduct;
import com.houdoudou.customer.R;
import com.houdoudou.customer.common.AsynImageLoader;
import com.houdoudou.customer.common.CommonFunction;
import com.houdoudou.customer.common.ListViewUtil;
import com.houdoudou.customer.common.UniversalHttpPost;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class OrderDetailFrg  extends Fragment{

	ListView productListView;
	
    ListAdapter productAdapter;
	AsynImageLoader imageLoader;
	TextView shopName, serviceTelnum;
	TextView customer,address,timeAA;
	TextView moneyProduct,moneyFreight,moneyCu;
	TextView productnum,productVolume;
	EditText note;

	Button confirm;
	View.OnClickListener myClickListener;
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.frg_orderdetail, container, false);
		
        imageLoader= new AsynImageLoader(getActivity());
		
		productListView= (ListView) view.findViewById(R.id.listview_frg_orderdetail);
	
		moneyCu= (TextView)view.findViewById(R.id.tv_moneycu_frg_orderdetail);
		
		shopName= (TextView)view.findViewById(R.id.tv_shopname_frg_orderdetail);
		serviceTelnum=(TextView)view.findViewById(R.id.tv_servicetel_frg_orderdetail);
		customer=(TextView)view.findViewById(R.id.tv_customertel_frg_orderdetail);
		address=(TextView)view.findViewById(R.id.tv_machineaddress_frg_orderdetail);
		timeAA=(TextView)view.findViewById(R.id.tv_arrivetime_frg_orderdetail);
		moneyProduct=(TextView)view.findViewById(R.id.tv_moneyproduct_frg_orderdetail);
		moneyFreight=(TextView)view.findViewById(R.id.tv_moneyfreight_frg_orderdetail);
		productVolume=(TextView)view.findViewById(R.id.tv_productvolume_frg_orderdetail);
		productnum=(TextView)view.findViewById(R.id.tv_productnum_frg_orderdetail);
		note= (EditText)view.findViewById(R.id.et_note_frg_orderdetail);
		note.setFocusable(false);
		
		productAdapter= new ProductListAdapter(getActivity());
		productListView.setAdapter(productAdapter);
		ListViewUtil.setListViewHeightBasedOnChildren(productListView);	
		

		moneyCu.setText("￥"+OrderDetailAct.orderJO.getDouble(Order.MONEYCU)); 
		moneyFreight.setText("￥"+OrderDetailAct.orderJO.getDouble(Order.MONEYFREIGHT)); 
	    moneyProduct.setText("￥"+OrderDetailAct.orderJO.getDouble(Order.MONEYPRODUCT)); 
	    shopName.setText(OrderDetailAct.orderJO.getString(Order.SUPPLIERNAME)); 
	    serviceTelnum.setText(OrderDetailAct.orderJO.getString(Order.SERVICETELNUM));
	    customer.setText("收货电话："+OrderDetailAct.orderJO.getString(Order.CUSTOMERTELNUM));
	    address.setText(OrderDetailAct.orderJO.getString(Order.MACHINEADDRESS));
	    timeAA.setText("送达时间："+OrderDetailAct.orderJO.getString(Order.DATETIMEAA));
	    productnum.setText(OrderDetailAct.orderJO.getString(Order.PRODUCTNUM));
	    productVolume.setText(OrderDetailAct.orderJO.getString(Order.VOLUME)+"升");
		note.setText(OrderDetailAct.orderJO.getString(Order.NOTE));
	    
		return view;
		
	}

	
	class ProductListAdapter extends BaseAdapter {
		   
		
	    private LayoutInflater layoutInflater;  
	    private Context context;  
	    private int parentPosition;
	        public ProductListAdapter(Context context) {
				
			this.context = context;
		    this.layoutInflater=LayoutInflater.from(context); 
		  
		}


	    final class Zujian{  
	         public TextView product,branch,price,num,volume; 
	       
	         public  ImageView img;
	      
	        }  
	          @Override
	       public int getCount() {
		// TODO Auto-generated method stub
		    return OrderDetailAct.productsJA.size();  
	        	 
	       }

	      @Override
	       public Object getItem(int position) {
		// TODO Auto-generated method stub
		       return OrderDetailAct.productsJA.getJSONObject(position);  
	    	 
	          }

	    @Override
	     public long getItemId(int position) {
		// TODO Auto-generated method stub
		       return position; 
	       }

	               @Override
	    public View getView(int position, View  convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int childID = position;   
		 Zujian zj=null;  
	        if(convertView==null){  
	            zj=new Zujian();  
	            //获得组件，实例化组件  
	            convertView=layoutInflater.inflate(R.layout.list_child_act_placeorder, null);  
	            zj.img=(ImageView) convertView.findViewById(R.id.img_product_listchild_actplaceorder);
	            
	            zj.num=(TextView) convertView.findViewById(R.id.tv_branchnum_listchild_placeorderact);
	            zj.branch=(TextView) convertView.findViewById(R.id.tv_branchname_listchild_placeorderact);
	            zj.product=(TextView) convertView.findViewById(R.id.tv_productname_listchild_placeorderact);
	            zj.price=(TextView) convertView.findViewById(R.id.tv_branchprice_listchild_placeorderact);
	            zj.volume=(TextView) convertView.findViewById(R.id.tv_branchvolume_listchild_placeorderact);
	            convertView.setTag(zj);  
	            System.out.println("position----"+position+"------inflated");
	        }else{   System.out.println("position----"+position+"-------not inflated");
	            zj=(Zujian)convertView.getTag();  
	        }  		        						  
	        //绑定数据  
	      JSONObject record=OrderDetailAct.productsJA.getJSONObject(position);
	        zj.num.setText(record.getInt(OrderProduct.BRANCHNUM)+"个");
	        zj.price.setText(record.getDouble(OrderProduct.BRANCHPRICE)+"元");
	        zj.volume.setText(record.getInt(OrderProduct.BRANCHVOLUME)+"升");
	        zj.branch.setText(record.getString(OrderProduct.BRANCHNAME));
	        zj.product.setText(record.getString(OrderProduct.PRODUCTNAME)); 
	        
	      imageLoader.showImageAsyn(zj.img, record.getString(OrderProduct.DIR), R.drawable.p3);       
	        return convertView;
	       } 
	         }// end Adapter

	
	
}
